package com.rongj.generation.data;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class DatabaseUtil {

    public static List<String> getColumn(String tableName){
        String sql = "select * from "+tableName+" limit 1";
        try {
            PreparedStatement pstmt = DateSourceManager.getConnction().prepareStatement(sql);
            ResultSet set = pstmt.executeQuery();
            ResultSetMetaData data = set.getMetaData();
            List<String> list = new ArrayList<String>();

            for(int i = 1;i<=data.getColumnCount();i++){
                list.add(data.getColumnName(i));
            }
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static List<String> getColumnType(String tableName){
        String sql = "select * from "+tableName+" limit 1";
        try {
            PreparedStatement pstmt = DateSourceManager.getConnction().prepareStatement(sql);
            ResultSet set = pstmt.executeQuery();
            ResultSetMetaData data = set.getMetaData();
            List<String> list = new ArrayList<String>();

            for(int i = 1;i<=data.getColumnCount();i++){
                list.add(data.getColumnTypeName(i));
            }
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
